home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util4 / 0utils.lha / 0Utils / gen.sasc.tplt < prev    next >
Text File  |  1995-08-19  |  11KB  |  408 lines

  1. $(@ITERATE@$(@lock@CMD@$(@OUTFILE@$(CMD).c@/******************************************************************************
  2. $(@HIDE@$(@outfile@@generating C source for $(CMD)
  3. ))
  4.     MODULE
  5.     $(CMD).c
  6.  
  7.     DESCRIPTION
  8. $(DESCRIPTION)
  9.  
  10.     NOTES
  11.     Kickstart 2.0+ required
  12.     compiles w/ SAS/C v6.51
  13.  
  14.     'C' Source automatically generated from $(CMD).data
  15.  
  16.     BUGS
  17. $(@IF@$(?BUGS)@$(BUGS))
  18.  
  19.     EXAMPLES
  20. $(@IF@$(?EXAMPLES)@$(EXAMPLES))
  21.  
  22.     HISTORY
  23. $(@IF@$(?HISTORY)@$(HISTORY))
  24.  
  25.     AUTHOR
  26.     Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
  27.     b_noll@informatik.uni-kl.de
  28.  
  29. ******************************************************************************/
  30.  
  31. /**************************************
  32.         Includes
  33. **************************************/
  34.  
  35. #ifndef   EXEC_LIBRARIES_H
  36. # include <exec/libraries.h>
  37. #endif /* EXEC_LIBRARIES_H */
  38.  
  39. #ifndef   EXEC_MEMORY_H
  40. # include <exec/memory.h>
  41. #endif /* EXEC_MEMORY_H */
  42.  
  43. #ifndef   CLIB_EXEC_PROTOS_H
  44. # include <clib/exec_protos.h>
  45. #endif /* CLIB_EXEC_PROTOS_H */
  46.  
  47. #ifndef   DOS_DOS_H
  48. # include <dos/dos.h>
  49. #endif /* DOS_DOS_H */
  50.  
  51. #ifndef   DOS_DOSASL_H
  52. # include <dos/dosasl.h>
  53. #endif /* DOS_DOSASL_H */
  54.  
  55. #ifndef   CLIB_DOS_PROTOS_H
  56. # include <clib/dos_protos.h>
  57. #endif /* CLIB_DOS_PROTOS_H */
  58.  
  59. #include <proto/dos.h>
  60. #include <proto/exec.h>
  61. $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  62. #include <proto/$(@TOLOWER@$(__name)).h>)))
  63.  
  64. $(@IF@$(?INCLUDES)@$(@WITH@INCLUDES@$(@ITERATE@
  65. #include $(__value))))
  66.  
  67. /**************************************
  68.      Defines & Structures
  69. **************************************/
  70.  
  71. #if !defined( WILDCARDFLAG ) /* <----------- Test for predefinition */
  72. # define WILDCARDFLAG    "/W"
  73. #endif
  74.  
  75. #if !defined( FORMATPREFIX ) /* <----------- Test for predefinition */
  76. # define FORMATPREFIX    "\0$ARG: "
  77. # define FORMATOFFSET    7
  78. #elif !defined( FORMATOFFSET )
  79. # define FORMATOFFSET    sizeof(FORMATPREFIX "")-1
  80. #endif
  81.  
  82. #define STDIN  "STDIN"
  83.  
  84. #define VERSIONPREFIX    "\0$VER: "
  85. #define VERSIONOFFSET    0
  86.  
  87. /* ---- We set the #ifdef around, since another    */
  88. /*    architecture might support another address */
  89. #ifndef   ABSEXECBASE
  90. #define   ABSEXECBASE ((struct ExecBase **)4L)
  91. #endif
  92.  
  93. #define MAXLINELEN  256
  94. #define MAXPATHLEN  256 /* BCPL won't allow bigger strings */
  95. #define EOF        -1    /* 'C' convention */
  96. #define ZERO         0L /* NULL for BPTRs */
  97.  
  98. #if defined( PATTERNMATCHING )
  99. # define WILDCARDADD    WILDCARDFLAG
  100. #else
  101. # define WILDCARDADD
  102. #endif
  103.  
  104. #define BUFFERSIZE     MAXLINELEN
  105. $(@IF@$(?BUFFERS)@
  106. #ifndef STATIC_BUFFERS
  107. #undef    BUFFERSIZE
  108. #define BUFFERSIZE     *argv->bufsize
  109. #endif)
  110.  
  111. struct _gvars {
  112.     struct DosLibrary *_DOSBase;
  113.     struct ExecBase   *_SysBase;
  114. $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  115.     //struct Library *_$(__name)Base;)))
  116.     struct {
  117. /* *************************** BEGIN USER FORMAT *************************** */
  118. /* "$(@IF@$(?TEMPLATE)@$(TEMPLATE),)"              /* CLI FORMAT */
  119.                             /* ****** STRUCTURES */
  120. $(@IF@$(?ARGUMENTS)@$(ARGUMENTS))$(@IF@$(?ARGS)@$(ARGS))
  121.  
  122. /* **************************** END USER FORMAT **************************** */
  123. $(@IF@$(?Filter)@
  124.     STRPTR *file;
  125.     STRPTR    to;
  126. )$(@IF@$(?BUFFERS)@        ULONG *bufsize;)
  127.     int dummy; // Just to make sure this structure is not empty
  128.     } _argv;
  129.     //struct ExecBase    *_SysBase;
  130.     //struct DosLibrary *_DOSBase;
  131. $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  132.     struct Library *_$(__name)Base;)))
  133. $(@IF@$(?Filter)@
  134.     BPTR    output;
  135.     BPTR    input;
  136.     int     infnum;
  137.     STRPTR  inname;)
  138.     ULONG   breakflag;
  139. #   if defined( PATTERNMATCHING )
  140.     struct AnchorPath *ap;
  141.     UBYTE  _ap[sizeof(struct AnchorPath) + MAXPATHLEN + 4];
  142.     ULONG    ap__ap_error;
  143. #   endif
  144.     struct {
  145. /* *************************** BEGIN USER FIELD **************************** */
  146.                             /* ****** STRUCTURES */
  147. $(@IF@$(?USERDATA)@$(USERDATA))
  148. $(@IF@$(?BUFFERS)@
  149. #ifndef STATIC_BUFFERS $(@WITH@BUFFERS@$(@ITERATE@
  150.     STRPTR $(__name);))
  151. #else $(@WITH@BUFFERS@$(@ITERATE@
  152.     UBYTE $(__name) [BUFFERSIZE];))
  153. #endif)
  154.  
  155. /* **************************** END USER FIELD ***************************** */
  156.     int ____dummy;
  157.     } _userdata;
  158. }; /* struct _gvars */
  159.  
  160. #define SysBase     gvars->_SysBase
  161. #define DOSBase     gvars->_DOSBase
  162. $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  163. #define $(__name)Base gvars->_$(__name)Base)))
  164.  
  165. #define outfile     gvars->output
  166. #define infile        gvars->input
  167. #define argv        (&gvars->_argv)
  168. #define userdata    (&gvars->_userdata)
  169.  
  170. #define BREAKCHECK() (gvars->breakflag |= CheckSignal(SIGBREAKF_CTRL_C))
  171. #define IS_BROKEN()  (gvars->breakflag)
  172.  
  173. /**************************************
  174.         Implementation
  175. **************************************/
  176.  
  177.  
  178. /* ---- we need that stub if compiling w/o __inline, since our */
  179. /*    commands start w/ the first instruction of this module */
  180. /*    if we compile w/ inlining, the function disappears     */
  181.  
  182. static __inline long _start(void) {
  183.     long _main(void);
  184.     return _main();
  185. } /* _start */
  186.  
  187.  
  188. $(@IF@$(?APPEND)||$(?ADDES)@
  189. /* *************************** BEGIN USER ADDES **************************** */
  190.  
  191. $(@IF@$(?APPEND)@$(APPEND))
  192. $(@IF@$(?ADDES)@$(ADDES))
  193.  
  194. #define SysBase     gvars->_SysBase
  195. #define DOSBase     gvars->_DOSBase
  196. /* **************************** END USER ADDES **************************** */)
  197.  
  198. $(@IF@$(?INIT)@
  199. /* ---- Do allocations etc. */
  200. static __inline long $(CMD)Init (struct _gvars *gvars) {
  201.     long retval = 0;
  202. /* **************************** BEGIN USER INIT **************************** */
  203.  
  204. $(INIT)
  205.  
  206. /* ***************************** END USER INIT ***************************** */
  207.     return retval;
  208. } /* $(CMD)Init */
  209. )
  210.  
  211. /* ---- main function called for each input file */
  212. static __inline long $(CMD)Fnc (struct _gvars *gvars) {
  213.     long retval = 0;$(@IF@$(?BODY)@
  214. /* **************************** BEGIN USER BODY **************************** */
  215.  
  216. $(BODY)
  217.  
  218. /* ***************************** END USER BODY ***************************** */)
  219.     return retval;
  220. } /* $(CMD)Fnc */
  221.  
  222. $(@IF@$(?EXIT)@
  223. /* ---- Do Disposes etc. */
  224. static __inline void $(CMD)Exit (struct _gvars *gvars) {
  225.     /* long retval = 0; */
  226. /* **************************** BEGIN USER EXIT **************************** */
  227.  
  228. $(EXIT)
  229.  
  230. /* ***************************** END USER EXIT ***************************** */
  231.     /* return retval; */
  232. } /* $(CMD)Exit */
  233. )
  234.  
  235. $(@If@$(?Filter)@
  236. /* ---- Select the First/next input file */
  237. static __inline BPTR _nextfile (struct _gvars *gvars, ULONG final) {
  238.     if (!argv->file) {
  239.     if (!infile)
  240.         infile = Input();
  241.     else
  242.         return FALSE;
  243.     gvars->inname = STDIN;
  244.     } else {
  245.     if (infile)
  246.         Close(infile);
  247.  
  248.     if (final)
  249.         return FALSE;
  250.  
  251.     if (gvars->breakflag || CheckSignal(SIGBREAKF_CTRL_C)) {
  252.         SetIoErr(ERROR_BREAK);
  253.         return FALSE;
  254.     }
  255.  
  256. #if !defined( PATTERNMATCHING )
  257.  
  258.     do {
  259.         STRPTR fn;
  260.         fn = gvars->inname = argv->file[gvars->infnum++];
  261.         if (!fn)
  262.         return FALSE;
  263.  
  264.         infile = Open(fn, MODE_OLDFILE);
  265.         PrintFault(IoErr(), fn);
  266.     } while (!infile);
  267. #else
  268. #warn "the current PATTERNMATCHING implementation is very big (+120 byte)"
  269.  
  270.     if (!infile) {
  271.         gvars->ap             = (void *)(((ULONG)gvars->_ap + 3) & ~3);
  272.         gvars->ap->ap_Strlen     = MAXPATHLEN;
  273.         gvars->inname         = gvars->ap->ap_Buf;
  274.         /*
  275.         gvars->ap->ap_BreakBits  = 0;
  276.         gvars->ap->ap_FoundBreak = 0;
  277.         gvars->ap__ap_error      = 0;
  278.         */
  279.         goto has_to_open_new_slot;
  280.     } /* if */
  281.  
  282. #define HAS_TO_OPEN_NEW_SLOT (gvars->ap__ap_error == ERROR_NO_MORE_ENTRIES)
  283.     infile = ZERO;
  284.     do {
  285.         STRPTR fn;
  286.         if (HAS_TO_OPEN_NEW_SLOT) {
  287.         //if (gvars->ap__ap_error == ERROR_NO_MORE_ENTRIES)
  288.             MatchEnd(gvars->ap);
  289.  
  290. has_to_open_new_slot:
  291.         if (!(fn = argv->file[gvars->infnum++]))
  292.             return 0;
  293.  
  294.         gvars->ap->ap_Flags = 0;
  295.         gvars->ap__ap_error = MatchFirst(fn, gvars->ap);
  296.         } else {
  297.         gvars->ap__ap_error = MatchNext(gvars->ap);
  298.         } /* if */
  299.  
  300.         if (!gvars->ap__ap_error)
  301.         infile = Open(fn = gvars->inname, MODE_OLDFILE);
  302.         if (gvars->ap__ap_error != ERROR_NO_MORE_ENTRIES)
  303.         PrintFault(IoErr(), fn);
  304.     } while (!infile);
  305.  
  306. #endif
  307.     } /* if */
  308.     return infile;
  309. } /* _nextfile */
  310. )
  311.  
  312. long _main (void)
  313. {
  314.     const char* version =
  315.            VERSIONPREFIX "$(CMD) $(VERSION) " __AMIGADATE__ + VERSIONOFFSET;
  316.     long retval = 20;
  317.  
  318. #ifdef STACK_VARS
  319.     struct _gvars _gvars  = { 0 };
  320. # ifdef FIX_SASPP
  321.     struct _gvars *gvars = (&_gvars);
  322. # else
  323. #   define gvars (&_gvars)
  324. # endif
  325.  
  326.     SysBase = *((struct ExecBase**)ABSEXECBASE);
  327. #else
  328.     //struct _gvars *gvars = (struct _gvars *)(-(LONG)sizeof(argv));
  329.     struct _gvars *gvars = NULL;
  330.  
  331. # if defined( FIX_SASOPT ) && defined( __USE_SYSBASE )
  332. #  undef SysBase
  333.     struct ExecBase *SysBase = *((struct ExecBase**)ABSEXECBASE);
  334.     //if ((ULONG)&SysBase == ABSEXECBASE)
  335. # endif
  336.     if (gvars = AllocVec(sizeof (*gvars), MEMF_CLEAR)) {
  337.     gvars->_SysBase = *((struct ExecBase**)ABSEXECBASE);
  338. #endif
  339.  
  340.     if (DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 37)) {
  341.         struct RDArgs *args;
  342. $(@IF@$(?BUFFERS)@#ifndef STATIC_BUFFERS
  343.         ULONG bufsize = MAXLINELEN;
  344.         argv->bufsize = &bufsize;
  345. #endif)
  346.  
  347. $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  348.         if (!($(__name)Base = OpenLibrary("$(@TOLOWER@$(__name)).library",37)))
  349.         goto closelibs;)))
  350.  
  351.         retval   = 10;
  352.         if (args = ReadArgs(FORMATPREFIX
  353.                 "$(@IF@$(?TEMPLATE)@$(TEMPLATE)$(@If@$(?Filter)@,))"
  354. $(@If@$(?Filter)@
  355.                 "FILE/M" WILDCARDADD
  356.                 ",TO/K"
  357. )$(@IF@$(?BUFFERS)@#ifndef STATIC_BUFFERS
  358.                 ",BUFFERSIZE/N/K"
  359. #endif
  360. )                + FORMATOFFSET,
  361.                    (LONG*)argv, NULL)) {
  362. $(@IF@$(?BUFFERS)@#ifndef STATIC_BUFFERS
  363.         bufsize = *argv->bufsize;$(@WITH@BUFFERS@$(@ITERATE@
  364.         if (!(userdata->$(__name) = AllocVec(bufsize, MEMF_CLEAR)))
  365.             goto freebuffers;))
  366. #endif)
  367.  
  368. $(@If@$(?Filter)@
  369.         outfile = argv->to ? Open(argv->to, MODE_NEWFILE) : Output();
  370.         if (outfile) {)
  371.             $(@if@!$(?INIT)@retval = 0;)$(@if@$(?INIT)@if (!(retval = $(CMD)Init(gvars))) {)$(@If@$(?Filter)@
  372.             while (_nextfile(gvars, retval)))
  373.                 retval = $(CMD)Fnc (gvars);
  374.             $(@if@$(?EXIT)@$(CMD)Exit(gvars);)
  375.             $(@if@$(?INIT)@} /* if */)
  376. $(@If@$(?Filter)@
  377.             if (argv->to)
  378.             Close(outfile);
  379.         } /* if */)
  380.  
  381. freebuffers:
  382. $(@IF@$(?BUFFERS)@#ifndef STATIC_BUFFERS$(@WITH@BUFFERS@$(@ITERATE@
  383.         if (userdata->$(__name))
  384.             FreeVec(userdata->$(__name));))
  385. #endif)
  386.  
  387.         FreeArgs (args);
  388.         } /* if */
  389.         if (retval)
  390.         PrintFault(IoErr(), "$(CMD)");
  391.  
  392.  
  393. closelibs: $(@IF@$(?LIBRARIES)@$(@WITH@LIBRARIES@$(@ITERATE@
  394.         if ($(__name)Base)
  395.         CloseLibrary($(__name)Base);)))
  396.         CloseLibrary ((struct Library *)DOSBase);
  397.     } /* if */
  398. #ifndef STACK_VARS
  399.     FreeVec (gvars);
  400.     } /* if */
  401. #endif
  402.     return retval;
  403. } /* _main */
  404.  
  405. /******************************************************************************
  406. *****  END $(CMD).c
  407. ******************************************************************************/)))
  408.